@tool
class_name Player
extends Character


@export var data: PlayerData        ## 设置该属性，会覆盖单独设置的对应属性的值
@export var gun: Gun

@export var score: int:
    set(value):
        if score == value: return
        score = value
        score_changed.emit()
            
@export var kill_count: int:
    set(value):
        if kill_count == value: return
        kill_count = value
        kill_count_changed.emit()
        
@export var gold: int


signal score_changed
signal kill_count_changed



func _ready() -> void:
    load_data()
    pass



func _physics_process(delta: float) -> void:
    if Engine.is_editor_hint(): return
    var direction := Input.get_vector("move_left", "move_right", "move_up", "move_down")
    velocity = velocity.move_toward(speed * direction, friction * delta)
    move_and_slide()
    
    var fire_direction := gun.fire_marker.global_position.direction_to(get_global_mouse_position())
    gun.fire(fire_direction)


func load_data() -> void:
    if data == null: return
    max_health = data.max_health
    health = data.health
    speed = data.speed
    scale = Vector2(data.scale, data.scale)
    gold = data.gold
    gun.bullet_damage = data.bullet_damage
    gun.bullet_speed = data.bullet_speed
    gun.bullet_radius = data.bullet_radius
    gun.bullet_knockback_force = data.bullet_knockback_force
    gun.fire_rate = data.gun_fire_rate
    gun.fire_bullet_count = data.gun_fire_bullet_count
    score = 0
    kill_count = 0
